package com.loveyukari.controller;

import com.loveyukari.common.exception.InternalException;
import com.loveyukari.dao.TourDao;
import com.loveyukari.dao.impl.BaseDao;
import com.loveyukari.dao.impl.JdbcRegisterDao;
import com.loveyukari.model.Auth;
import com.loveyukari.model.Register;
import com.loveyukari.model.TourUser;
import com.loveyukari.service.*;
import com.loveyukari.support.JsonResult;
import com.loveyukari.utils.AliYunUtils;
import com.loveyukari.utils.HttpUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.InetAddress;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
*
*yangshaohua
*2017年2月11日下午9:10:32
**/
@RequestMapping("/admin/test")
@Controller
public class TestController extends BaseDao{

	private Logger logger = LoggerFactory.getLogger(TestController.class);

	@Autowired
	private TourService tourService;
	@Autowired
	private TourDao tourDao;
	@Autowired
	private AuthService authService;
	@Autowired
	private IpCoolService ipCoolService;


	@RequestMapping
	public String test(Model model) {
		List<TourUser> list = tourDao.selectAll();
		for (TourUser tourUser :
				list) {
			Integer authId = tourUser.getAuthId();
			Auth auth = authService.getCacheAuth(authId);
			if (auth == null){
				continue;
			}
			try {
				int page = 1;
				while(true){
                    HashMap<String, Object> param = new HashMap<>();
                    param.put("uid",authId);
                    param.put("limit",30);
                    param.put("source",2);
                    param.put("page",page);
                    String result = HttpUtils.sendGet("https://yuba.douyu.com/wbapi/web/group/userFollow", param);
                    JSONObject json = JSONObject.fromObject(result);
                    JSONObject data = json.optJSONObject("data");
                    JSONArray array = data.optJSONArray("list");
                    for (int i = 0; i < array.size(); i++) {
                        JSONObject authYubaFollow = array.getJSONObject(i);
                        int groupId = authYubaFollow.optInt("group_id");
                        if(groupId == 588){
                            tourDao.updateTourExclude(auth.getId(),1);
                            break;
                        }
                    }
                    page++;
                    if (data.optInt("count_page") < page){
                        break;
                    }
                    Thread.sleep(500);
                }
			} catch (Exception e) {
				logger.error(e.getMessage(),e);
			}
		}

		return "test";
	}


}















